package com.five.matebook.activity.db;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

public class OneQueryData implements Parcelable {
    private Map<String, Object> mData;

    public OneQueryData(Map<String, Object> data) {
        mData = data;
    }

    // 读取数据的方法保持不变
    public String get(String key) {
        return (String) mData.get(key);
    }

    public long getLong(String key, long def) {
        String v = get(key);
        if (TextUtils.isEmpty(v)) {
            return def;
        }
        try {
            return Long.parseLong(v);
        } catch (NumberFormatException e) {
            return def;
        }
    }

    public float getFloat(String key, float def) {
        String v = get(key);
        if (TextUtils.isEmpty(v)) {
            return def;
        }
        try {
            DecimalFormat decimalFormat = new DecimalFormat("0.00");
            return Float.parseFloat(decimalFormat.format(Float.parseFloat(v)));
        } catch (NumberFormatException e) {
            return def;
        }
    }

    // Parcelable implementation
    protected OneQueryData(Parcel in) {
        int size = in.readInt();
        mData = new HashMap<>(size);
        for (int i = 0; i < size; i++) {
            String key = in.readString();
            Object value = in.readValue(ClassLoader.getSystemClassLoader());
            mData.put(key, value);
        }
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(mData.size());
        for (Map.Entry<String, Object> entry : mData.entrySet()) {
            dest.writeString(entry.getKey());
            dest.writeValue(entry.getValue());
        }
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<OneQueryData> CREATOR = new Creator<OneQueryData>() {
        @Override
        public OneQueryData createFromParcel(Parcel in) {
            return new OneQueryData(in);
        }

        @Override
        public OneQueryData[] newArray(int size) {
            return new OneQueryData[size];
        }
    };
}
